﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.IT.Crm.Utility.CrmSolutionManager
{
    class Program
    {
        /// <summary>
        /// Input parameter should include:
        /// /i or /e indicates import or export.
        /// /type:standard or /type:customized or /type:xml
        /// /domain:domain
        /// /username:username
        /// /password:***
        /// /server:serverurl
        /// /port:443
        /// /ssl:true
        /// /organizationname:organization1
        /// /output:solution.zip
        /// /managed:false
        /// /publisher:publisher
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            if (args.Length == 1 && args[0] == "/?")
                PrintHelpInformation();
            else
            {
                if (!ValidateInputParameters(args))
                    PrintInvalidParameters(args);
                else
                {
                    if (CRMSolutionManagerConfig.Instance.SolutionImportFlag == SolutionImportFlag.Export)
                    {
                        EnterpriseLibLogger.Log("Creating the connection service proxy.");
                        var solutionExporter = SolutionExporterFactory.CreateSolutionExporter();

                        EnterpriseLibLogger.Log("Start: Exporting the solutions.");
                        var managed = GetSolutionExportManaged();
                        solutionExporter.Export(managed == OutputManagedFlag.Managed);

                        EnterpriseLibLogger.Log("End: Exporting the solutions.");
                    }
                    else
                    {
                        EnterpriseLibLogger.Log("Creating the connection service proxy.");
                        var solutionImporter = SolutionImporterFactory.CreateSolutionImporter();
                        EnterpriseLibLogger.Log("Start: Importing the solutions.");
                        solutionImporter.Import();

                        EnterpriseLibLogger.Log("End: Importing the solutions.");
                    }
                }
            }
#if DEBUG
            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
#endif
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject != null)
                EnterpriseLibLogger.Log(((Exception)e.ExceptionObject).Message);
            if (e.IsTerminating)
            {
                EnterpriseLibLogger.Log("Application is going to terminating!");
                Console.ReadLine();
            }
        }

        private static OutputManagedFlag GetSolutionExportManaged()
        {
            return CRMSolutionManagerConfig.Instance.OutputManagedFlag;
        }

        private static void PrintHelpInformation()
        {
            // TBD
        }

        private static bool ValidateInputParameters(string[] args)
        {
            if (args.Length == 0) return false;
            else
                try
                {
                    var config = args[0];
                    CRMSolutionManagerConfig.Initialize(config);
                    return true;
                }
                catch
                {
                    return false;
                }
        }

        private static void PrintInvalidParameters(string[] args)
        {
            // TBD
        }
    }
}
